//共有消息提示
(function(){
    var alertTpl1='<div class="alert alert-danger"><span></span><button type="button" class="close" >&times;</button></div>';
    var modalTpl='<div class="modal fade" tabindex="-1" role="dialog">\n'+
        '<div class="modal-dialog modal-sm" role="document">\n'+
        '<div class="modal-content">\n'+
        '<div class="modal-header">\n'+
        '<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>\n'+
        '<h4 class="modal-title"></h4>\n'+
        '</div>\n'+
        '<div class="modal-body">\n'+
        '<p>One fine body&hellip;</p>\n'+
        '</div>\n'+
        '<div class="modal-footer">\n'+
        '<button type="button" class="btn btn-default">确定</button>\n'+
        '</div>\n'+
        '</div><!-- /.modal-content -->\n'+
        '</div><!-- /.modal-dialog -->\n'+
        '</div><!-- /.modal -->';

    var modalDialog=false;

    jQuery.extend({
        modal:function(message,title,onOk){
            if(!modalDialog){
                modalDialog=$(modalTpl);
            }
            modalDialog.find('.modal-title').html(title||'消息');
            modalDialog.find('.modal-body').html(message);
            modalDialog.modal('show');
            modalDialog.find('.btn').unbind('click').click(function(){
                modalDialog.modal('hide');
                if(onOk){
                    onOk();
                }
            })
        }
    });

    jQuery.fn.extend({
        showAlert:function(mes){
            var alertTpl = $(alertTpl1);
            alertTpl.find('span').html(mes);
            alertTpl.prependTo(this);
            alertTpl.click(function(){
                alertTpl.remove();
            });
            setTimeout(function(){
                alertTpl.remove();
            },3000);
        }
    });

    jQuery.ajaxSetup({
        error:function(e){
            switch (e.status){
                case 403:
                    $.modal('请登录后访问','未登录',function(){
                        location = '';
                    });
                    break;
                default:
                    $.modal('和服务器交换数据错误')
            }
        }
    })

})();
